package com.onlinelibrary.activities.collection;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Application;
import android.content.Intent;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.lifecycle.AndroidViewModel;

import com.onlinelibrary.activities.App;
import com.onlinelibrary.activities.position.PositionActivity;
import com.onlinelibrary.adapter.CollectAdapter;
import com.onlinelibrary.bean.CollectionBean;
import com.onlinelibrary.databinding.ActivityCollectBinding;
import com.onlinelibrary.utils.Http;
import com.onlinelibrary.view.PullingLayout;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class CollectVM extends AndroidViewModel {
    private static final List<CollectionBean> data=new ArrayList<>();
    @SuppressLint("StaticFieldLeak")
    private static CollectActivity collectActivity;
    @SuppressLint("StaticFieldLeak")
    private static ActivityCollectBinding binding;
    public CollectVM(@NonNull @NotNull Application application) {
        super(application);
    }
    public void setBinding(ActivityCollectBinding binding, CollectActivity collectActivity) {
        CollectVM.collectActivity =collectActivity;
        CollectVM.binding =binding;
        binding.setAdp(new CollectAdapter(collectActivity,data));
        binding.collectList.setOnItemLongClickListener((parent, view, position,id)->{
            AlertDialog.Builder dialog=new AlertDialog.Builder(collectActivity);
            dialog.setTitle("确定取消收藏吗?").setPositiveButton("取消收藏", (dialog1, which) -> Http.get("collect/delete/?posName="+ data.get(position).position +"&email=" + data.get(position).email, new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e){}
                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) {
                    collectActivity.runOnUiThread(() -> Toast.makeText(collectActivity,"取消收藏成功",Toast.LENGTH_LONG).show());
                    refresh(null);
                }
            })).setNegativeButton("关闭",null).show();
            return true;
        });
        binding.collectList.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent=new Intent(collectActivity, PositionActivity.class);
            intent.putExtra("pos",data.get(position).position);
            collectActivity.startActivity(intent);
        });
        refresh(null);
    }
    private static void refresh(PullingLayout pullingLayout) {
        data.clear();
        Http.get("collect/get/?type=getAll&email=" + App.sp.getString("account",""), new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) { }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                try {
                    assert response.body() != null;
                    JSONArray js = new JSONArray(response.body().string());
                    for (int i = 0; i < js.length(); i++) {
                        JSONObject ja = js.getJSONObject(i);
                        data.add(new CollectionBean(ja.getString("email"),ja.getString("posName")));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                collectActivity.runOnUiThread(() ->{
                    binding.getAdp().notifyDataSetChanged();
                    if (pullingLayout!=null)pullingLayout.refreshFinish(0);
                });
            }
        });
    }
    @BindingAdapter("bindPull")
    public static void bindPull(PullingLayout pullingLayout,CollectVM collectVM){
        pullingLayout.setPullDownEnabled(true);
        pullingLayout.setOnRefreshListener(CollectVM::refresh);
    }
}
